<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/28 0028
 * Time: 下午 3:26
 */
namespace Repositories;

class QrcodeRep
{
    protected $codeInfo;
    protected $merchant;
    protected $batch;
    protected $code;

    public function __construct($code)
    {
        $this->code = $code;
        $this->codeInfo = $this->decode($code);
        $this->initMerchant();
        $this->initBatch();
    }

    protected function initMerchant(){
        $map['code'] = $this->codeInfo['mch_code'];
        $this->merchant = M('merchants')->where($map)->find();
    }

    protected function initBatch(){
        $map['mchId'] = $this->merchant['id'];
        $value = $this->codeInfo['value'];
        $map['start'] = ['ELT',$value];
        $map['end'] = ['EGT',$value];
        $this->batCh = M('batchs')->where($map)->find();
    }

    public function getMerchant(){
        return $this->merchant;
    }

    public function getCodeInfo(){
        return $this->codeInfo;
    }


    public function initStatus(){
        if (!$this->merchant) {        //记表common_log
            return $this->setLogTypeStatus(LogTypes::SCAN_ILLEGAL,QrcodeStatus::NO_MCH);
        }
        if (!$this->batCh) {                             //记表common_log
            return $this->setLogTypeStatus(LogTypes::SCAN_ILLEGAL,QrcodeStatus::NO_BATCH);
        }
        if ($this->batCh['state'] == 0) {                 //记表common_log
            return $this->setLogTypeStatus(LogTypes::SCAN_ILLEGAL,QrcodeStatus::NO_ACTIVE);
        }
        if(session('userInfo')){
            $user = session('userInfo');
            $scanLogFactory = ScanLogManager::createScanLog(config('app.scan_log_driver','database'),$this->code);
            $scanLog = $scanLogFactory->getScanLogByCode();

            if ($scanLog && $scanLog->getOpenId() == $user['original']['openid']) { //记表common_log
                return $this->setLogTypeStatus(LogTypes::SCAN_AGAIN,QrcodeStatus::SCAN_AGAIN);
            }
            if ($scanLog && $scanLog->getOpenId() != $user['original']['openid']) {   //记表common_log
                return $this->setLogTypeStatus(LogTypes::SCAN_OTHER,QrcodeStatus::SCAN_OTHER);
            }
            if ($this->batCh->getState() == 2) {
                return $this->setLogTypeStatus(LogTypes::SCAN_STOP,QrcodeStatus::STOP);
            }
            if ($this->batCh->getExpireTime() < time()) {
                return $this->setLogTypeStatus(LogTypes::SCAN_OVERDUE,QrcodeStatus::OVERDUE);
            }
        }else{
            $commonLog = new UsersCommonLogFactory(null);
            $self = $commonLog->getSelfLogByCode($this->code);
            $other = $commonLog->getOtherLogByCode($this->code);
            if($self && $other){
                if($self->id > $other->id){
                    return $this->setLogTypeStatus(LogTypes::SCAN_AGAIN,QrcodeStatus::SCAN_AGAIN);
                }else{
                    return $this->setLogTypeStatus(LogTypes::SCAN_OTHER,QrcodeStatus::SCAN_OTHER);
                }
            }
            if(!$self && $other){
                return $this->setLogTypeStatus(LogTypes::SCAN_OTHER,QrcodeStatus::SCAN_OTHER);
                return $this;
            }
            if ($self && !$other){
                return $this->setLogTypeStatus(LogTypes::SCAN_AGAIN,QrcodeStatus::SCAN_AGAIN);
            }
            if ($this->batCh->getState() == 2) {
                return $this->setLogTypeStatus(LogTypes::SCAN_STOP,QrcodeStatus::STOP);
            }
            if ($this->batCh->getExpireTime() < time()) {
                return $this->setLogTypeStatus(LogTypes::SCAN_OVERDUE,QrcodeStatus::OVERDUE);
            }
        }
        return $this->setLogTypeStatus(LogTypes::SCAN_NORMAL,QrcodeStatus::NORMAL);
    }

    public function setLogTypeStatus($logType,$status){
        $this->logType = $logType;
        $this->status = $status;
        return $this;
    }

    public function decode($code){
        $param = [
            'code'          => $code,
            'version'       => '3',
            'mch_code_len'  => 2,
            'serial_len'    => 6,
            'valid_len'     => 2
        ];
        return hls_decode($param);
    }


}